//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_killedeh.sqf"
#include "xr_macros.sqf"

__TRACE_1("start","_this");
__pSetVar [QGVARXR(presptime), 6];
setPlayerRespawnTime 6;
__TRACE("respawn time 6");
__pSetVar [QGVARXR(death_pos), []];
_do_black = true;
if (__pGetVar(GVARXR(pluncon))) then {
	call FUNCXR(CheckRespawn);
} else {
	__pSetVar [QGVARXR(pluncon), true, true]; // TODO remove again ? Set in respawneh
	if (d_sub_kill_points != 0) then {
		[QGVARXR(add_sub_kill), player] call d_fnc_NetCallEventCTS;
	};
	private "_pdx";
	_pdx = __pGetVar(GVARXR(num_death));
	__INC(_pdx);
	__TRACE_1("","_pdx");
	__pSetVar [QGVARXR(num_death), _pdx];
	
	if (GVARXR(max_lives) != -1) then {
		private "_lives";
		_lives = __pGetVar(GVARXR(lives)) - 1;
		__TRACE_1("lives left","_lives");
		__pSetVar [QGVARXR(lives), _lives];
		["d_crl", [getPlayerUID player, _lives]] call d_fnc_NetCallEventCTS;
		if (_lives == -1) then {
			__TRACE("lives = -1");
			__pSetVar [QGVARXR(isdead), true];
			// TODO
			// normal respawn EH resets mags and weapons after respawn
			GVARXR(phd_invulnerable) = true;
			[true] spawn FUNCXR(park_player);
			player removeAllEventHandlers "killed";
			player removeAllEventHandlers "respawn";
			_do_black = false;
		};
	};
};
__TRACE_1("","_do_black");
if (_do_black) then {
	0 spawn {
		_etime = time + __pGetVar(GVARXR(presptime)) - 1.3;
		__TRACE_1("spawn","_etime");
		waitUntil {time >= _etime};
		__TRACE("spawn blacking out 1 sec before respawn");
		172 cutText ["","BLACK OUT", 1];
	};
};